package com.gexiaobao.pigeon.ui.fragment.match.training

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentPickUpPigeonBinding
import com.gexiaobao.pigeon.ui.adapter.PickUpDetailAdapter
import com.gexiaobao.pigeon.viewmodel.TrainingInfoViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 11:04
 * @Describe    : 接鸽详情
 */
class FragmentPickUpPigeon : BaseFragment<TrainingInfoViewModel, FragmentPickUpPigeonBinding>(){

    private val mAdapter: PickUpDetailAdapter by lazy { PickUpDetailAdapter() }
    private lateinit var loadsir: LoadService<Any>
    private var mRaceId = 0

    override fun initView(savedInstanceState: Bundle?) {
        mRaceId = requireArguments().getInt("raceId")
        val title = arguments?.getString("title")
        title?.let {
            mDatabind.includeBar.toolbar.initClose(it) {
                nav().navigateUp()
            }
        }
    }

    override fun initData() {
        super.initData()
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).addItemDecoration(
            SpaceItemDecoration(0, Util.dp2px(context,2f))
        )

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getGatherPost(mRaceId.toString())
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getGatherPost(mRaceId.toString())
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.getGatherPost(mRaceId.toString())
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.pickUpPigeonResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
        }
    }
}